package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.os.Bundle;
import android.view.View;

import java.util.Date;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;

/**
 * Created by zxh on 16/11/25.
 */

public class ArticleItem extends BindingListItem {
    private int articleId;
    private String title;
    private Date collectTime;
    private int browser;
    private String content;
    private String author;
    private String avatar;
    private String tag;
    private String description;
    private int replycoun;
    private int likecoun;
    private int uid;
    private String label;
    private Date uploadtime;
    private String systime;
    private ListBindingAdapter imageList;


    @Bindable
    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    /**
     * 文章作者
     *
     * @return
     */
    @Bindable
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getArticleId() {
        return articleId;
    }

    public void setArticleId(int articleId) {
        this.articleId = articleId;
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        notifyPropertyChanged(BR.title);
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
        notifyPropertyChanged(BR.content);
    }

    @Bindable
    public Date getCollectTime() {
        return collectTime;
    }

    public void setCollectTime(Date collectTime) {
        this.collectTime = collectTime;
        notifyPropertyChanged(BR.collecttime);
    }

    @Bindable
    public int getBrowser() {
        return browser;
    }

    public void setBrowser(int browser) {
        this.browser = browser;
        notifyPropertyChanged(BR.browser);
    }

    /**
     * 获取用户头像
     *
     * @return
     */
    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
        notifyPropertyChanged(BR.avatar);
    }

    /**
     * 获取文章简介
     *
     * @return
     */
    @Bindable
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
        notifyPropertyChanged(BR.description);
    }

    /**
     * 获取回复人数
     *
     * @return
     */
    @Bindable
    public int getReplycoun() {
        return replycoun;
    }

    public void setReplycoun(int replycoun) {
        this.replycoun = replycoun;
        notifyPropertyChanged(BR.replycoun);
    }

    /**
     * 获取点赞人数
     *
     * @return
     */
    @Bindable
    public int getLikecoun() {
        return likecoun;
    }

    public void setLikecoun(int likecoun) {
        this.likecoun = likecoun;
        notifyPropertyChanged(BR.likecoun);
    }

    /**
     * 获取文章图片列表
     *
     * @return
     */
    @Bindable
    public ListBindingAdapter getImageList() {
        return imageList;
    }

    /**
     * 获取作者标签（新人、专家。。。）
     *
     * @return
     */
    @Bindable
    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
        notifyPropertyChanged(BR.tag);
        notifyPropertyChanged(BR.showJob);
    }

    @Bindable
    public boolean getShowJob() {
        return tag != null && !"".equals(tag);
    }

    /**
     * 获取文章上传时间
     *
     * @return
     */
    @Bindable
    public Date getUploadtime() {
        return uploadtime;
    }

    public void setUploadtime(Date uploadtime) {
        this.uploadtime = uploadtime;
        notifyPropertyChanged(BR.uploadtime);
    }

    @Bindable
    public String getSystime() {
        return systime;
    }

    public void setSystime(String systime) {
        this.systime = systime;
    }

    /**
     * 获取用户id
     *
     * @return
     */
    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public ArticleItem(Context context, int layoutId) {
        super(context, layoutId);
        imageList = new ListBindingAdapter(context, 0);
    }

    /**
     * 点击用户头像
     *
     * @param view
     */
    public void onClickAvatar(View view) {
        HomeActivity.showPersonalCentral(getContext(), uid);
    }

    /**
     * 点击文章时显示文章详情
     *
     * @param view
     */
    public void onClickItem(View view) {
        ArticleDetail detail = new ArticleDetail(getContext(), this);
        Activity activity = (Activity) view.getContext();
        CommonFragment fragment = new CommonFragment();
        fragment.setLayoutId(R.layout.f_article_detail);
        fragment.setVariableId(BR.detail);
        fragment.setDataModel(detail);
        HomeActivity.switchFragment(activity, fragment);
    }

    /**
     * 点击赞
     *
     * @param view
     */
    public void onClickLike(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = WebApi.docLike(getContext(), getArticleId());
                if (count == -1) {
                    return;
                } else {
                    setLikecoun(count);
                }
            }
        }).start();
    }
}
